package ngat.oss;

import ngat.message.OSS.*;
import ngat.util.logging.*;
import ngat.phase2.*; 
import ngat.instrument.*;
import ngat.phase2.nonpersist.*;

import java.util.*;
import java.io.*;

import jyd.dbms.*;
import jyd.storable.*;
import jyd.collection.*;

/**
 * Client: USR
 * Action: Sent by USR to retreive status information about a Proposal. 
 * @author S.N.Fraser
 * $Id: GET_PROPOSAL_STATUSImpl.java,v 1.1 2006/11/27 08:45:00 snf Exp $
 */
public class GET_PROPOSAL_STATUSImpl extends TransactionImpl {

    /** The path used to identify the Proposal in the OSS database. */
    protected Path proposalPath;

    public GET_PROPOSAL_STATUSImpl(Path proposalPath) {
	super();
	this.proposalPath = proposalPath;
    }

    /** Create a GET_PROPOSAL_STATUSImpl for the supplied request.*/
    public GET_PROPOSAL_STATUSImpl(GET_PROPOSAL_STATUS request) {
	super(request);
	this.proposalPath = request.getProposalPath();
    }
    
    /**
     * Retrieves the Proposal specified
     * @return Proposal or an OSS_Error.
     */
    public TRANSACTION_DONE exec() { 

	Proposal proposal = null;
	
	Transaction t = null;
	try {
	    t = Transaction.current();
	    
	    // Fetch the Proposal.
	    proposal = null;
	    try {
		proposal = OSS_Utilities.fetchProposal(proposalPath);
	    } catch (IOException iox) {
		logger.log(1, CLASS, id, "exec",
			   "Proposal: "+proposalPath.toString()+
			   " Error fetching from Database: "+iox);
		return error(TRANSACTION.DATABASE_READ_ERROR,			   
				     "Proposal: "+proposalPath.toString()+
				     " Error reading from database: "+iox);
	    }		
	    
	    // Check Proposal exists.
	    if (proposal == null) {		
		// Couldn't find the Proposal.
		logger.log(1, "GET_PROPOSAL_STATUSImpl", id, "exec", 
			   "Proposal: "+proposalPath.toString()+" Not in database.");
		return error(TRANSACTION.PATH_RESOLUTION_ERROR, 
				     "Could not find Proposal: "+proposalPath.toString());
	    } 
	    
	} finally {
	    t.commit();
	}
	
	ProposalDescriptor desc = proposal.getDescriptor();
	
	GET_PROPOSAL_STATUS_DONE done = new GET_PROPOSAL_STATUS_DONE(id);
	done.setDescriptor(desc);
	done.setSuccessful(true);
	return done;

    }

} // Class Def. [USR-GetProposalStatus_Req].

/** $Log: GET_PROPOSAL_STATUSImpl.java,v $
/** Revision 1.1  2006/11/27 08:45:00  snf
/** Initial revision
/**
/** Revision 1.1  2001/02/23 18:51:14  snf
/** Initial revision
/**
/** Revision 1.1  2001/02/19 17:31:49  snf
/** Initial revision
/**
/** Revision 1.1  2000/12/12 18:18:32  snf
/** Initial revision
/** */
